package kiwi.assist.dev.guieditor.mediator {
	import kiwi.util.ArrayUtils;
	import kiwi.assist.dev.util.vectoryToArray;
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.view.UiList;

	import org.aswing.event.ListItemEvent;
	import org.robotlegs.mvcs.Mediator;

	/**
	 * @author zhangming.luo
	 */
	public class UiListMediator extends Mediator {
		[Inject]
		public var view : UiList;
		[Inject]
		public var config : GUIEditorConfig;

		override public function onRegister() : void {
			super.onRegister();
			addContextListener(GUIEditorEvent.CONFIG_CHANGE, handleConfigChange);
			view.addEventListener(ListItemEvent.ITEM_DOUBLE_CLICK, handleDoubleClick);
			view.addEventListener(ListItemEvent.ITEM_CLICK, handleClick);
		}

		private function handleConfigChange(event : GUIEditorEvent) : void {
			view.setListData(ArrayUtils.reject(vectoryToArray(config.uis), "hide", true), "type");
		}

		private function handleClick(event : ListItemEvent) : void {
		}

		private function handleDoubleClick(event : ListItemEvent) : void {
			var uiEvent : GUIEditorEvent = new GUIEditorEvent(GUIEditorEvent.UI_CREATE);
			uiEvent.data = event.getValue();
			eventDispatcher.dispatchEvent(uiEvent);
		}
	}
}
